package org.blogwu.learn.algorithm.树.二叉搜索树;

import org.blogwu.learn.algorithm.树.二叉搜索树.printer.BinaryTrees;

import java.util.Comparator;

public class TreeTest {
    public static void main(String[] args) {
//        Integer[] ints = new Integer[]{
//                7, 4, 2, 3, 5, 9, 8, 11, 12
//        };
        Integer[] ints = new Integer[]{
                11, 24, 7, 4, 25, 21, 6, 20, 30
        };
        BinarySearchTree<Integer> tree = new BinarySearchTree<>();
        for (int i = 0; i < ints.length; i++) {
            tree.add(ints[i]);
        }

        BinaryTrees.print(tree);
        System.out.println("");
        System.out.println("-----------");
        BinarySearchTree<Person> tree2 = new BinarySearchTree<>(new Comparator<Person>() {
            @Override
            public int compare(Person o1, Person o2) {
                return o2.getAge() - o1.getAge();
            }
        }
        );
        for (int i = 0; i < ints.length; i++) {
            tree2.add(new Person(ints[i]));
        }
        BinaryTrees.print(tree2);
    }

    /**
     * 创建一个二叉树
     * @return
     */
    public static BinarySearchTree<Integer> getTreeAndPrint() {
        Integer[] ints = new Integer[]{
//                11, 24, 7, 4, 25, 21, 6, 20, 30
                50, 19, 24, 27, 48, 5, 7, 3, 40
        };
        BinarySearchTree<Integer> tree = new BinarySearchTree<>();
        for (int i = 0; i < ints.length; i++) {
            tree.add(ints[i]);
        }

        BinaryTrees.print(tree);
        return tree;
    }

    /**
     * 完全二叉树
     *
     * @return
     */
    public static BinarySearchTree<Integer> getCompleteTreeAndPrint() {
        Integer[] ints = new Integer[]{
                39, 50, 35, 10, 46, 36, 18, 9, 51  //是
//                39, 50, 35, 10, 46, 36, 18,  9 ,51,37  // no
//                42, 50, 35, 10, 46, 38, 18,  9 ,51,37 // yes
//                42, 50, 35, 10, 46, 38, 18,  9 ,51,39 // no
//                42, 50, 35, 10, 46, 38, 18,  9 ,51,39,37,43 // yes
//                42, 50, 35, 10, 46, 38, 18,  9 ,51,39,37,43,47// yes
//                42, 50, 35, 10, 46, 38, 18,  9 ,39,37,43,47 // false
//                2,1,7,3,8 // false
        };
        BinarySearchTree<Integer> tree = new BinarySearchTree<>();
        for (int i = 0; i < ints.length; i++) {
            tree.add(ints[i]);
        }

        BinaryTrees.print(tree);
        return tree;
    }


}
